package com.erpoa.controller;

import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessage;
import com.erpoa.service.FactoryUtilService;
import com.erpoa.service.PackServletService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenjimin
 * @Title: PackServletoller
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2021/2/1810:17
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "PackServletController", tags = {"定量包装pda接口"})
@RestController
@RequestMapping("/PDAService")
public class PackServletController {
    private static final Logger logger = LoggerFactory.getLogger(PackServletController.class);
    @Autowired
    private PackServletService packServletService;
    @Autowired
    private FactoryUtilService factoryUtilService;


    @ApiOperation("定量包装pda接口")
    @RequestMapping(value = {"/packservlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    // @NoRepeatSubmit(key = "packservlet:arg[0]")
    public DealMessage packservlet(@RequestBody PackMessage packMessage) {

        boolean isSuccess = false;
        DealMessage dealMessage = new DealMessage(true);
        isSuccess = packServletService.functionOps(packMessage, dealMessage, 45);
        try {
            int mBarTotalNum = factoryUtilService.getBarCodeTotalNum(packMessage); // 条码的总数量
            if (!(mBarTotalNum == -1 || mBarTotalNum == 0)) {
                // TODO: //0:注塑 1:印刷 2:装配 3:油画棒4:笔芯二厂定量扫描
                switch (packMessage.getOptType()) {
                    case 1:// 1:注塑(单人报工)
                        isSuccess = packServletService.functionPlastic(packMessage, dealMessage, mBarTotalNum);
                        break;
                    case 0:// 0:印刷(单人报工)
                        isSuccess = packServletService.functionPrint(packMessage, dealMessage, mBarTotalNum);
                        break;
                    case 2:// 2:笔芯二厂机装(单人报工)
                        isSuccess = packServletService.functionAssemble(packMessage, dealMessage, mBarTotalNum);
                        break;
                    case 3:// 3:油画棒(单人报工)
                        //isSuccess = packServletService.functionOps(packMessage, dealMessage, mBarTotalNum);
                        break;
                    case 4:// 4:笔芯二厂装箱段(按组报工)
                        isSuccess = packServletService.functionRefillTwo(packMessage, dealMessage, mBarTotalNum);
                        break;
                }
            } else {
                dealMessage.setSuccess(false);
                dealMessage.setMessage("此条码不可扫描产出");
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof CannotSerializeTransactionException || e instanceof CannotCreateTransactionException) {
                dealMessage = new DealMessage(false, "数据正在处理中，请再试一次！");

            } else if (e instanceof TooManyResultsException) {
                dealMessage = new DealMessage(false, "数据错误，请检查！" + e.getMessage());
            } else {
                dealMessage = new DealMessage(false, "服务器hj接口出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }

        if (isSuccess) {// 成功，会传回True
            return dealMessage;
        } else {// 失败，回传失败原因
            return dealMessage;
        }

    }


}
